package net.zhengxinyun.performance.controller.workSupervision;

import com.sirdc.modules.utils.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.data.WorkSupervisionData;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.WorkSupervisionEntity;
import net.zhengxinyun.performance.filter.WorkSupervisionFilter;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.workSupervision.WorkSupervisionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;

/**
 * Created by hyp1202 on 2018/12/12.
 */
@RestController
@RequestMapping("/workSupervision")
public class WorkSupervisionController {

    private TokenService tokenService;
    private WorkSupervisionService workSupervisionService;

    @Autowired
    public WorkSupervisionController(TokenService tokenService, WorkSupervisionService workSupervisionService) {
        this.tokenService = tokenService;
        this.workSupervisionService = workSupervisionService;
    }

    @GetMapping(value = "/file")
    public String file() {
        return "file";
    }

    @PostMapping("/insert")
    public Result<String> insert(@RequestBody WorkParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            String str = workSupervisionService.insert(param.workSupervisionEntity);
            if (!"添加成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody WorkParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            String str = workSupervisionService.update(param.workSupervisionEntity);
            if (!"修改成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody WorkParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            String str = workSupervisionService.delete(param.workSupervisionEntity);
            if (!"删除成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/query")
    public Result<HashMap<String, Object>> query(@RequestBody WorkParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            List<WorkSupervisionFilter> list = workSupervisionService.query(param.workSupervisionFilter);
            if (list.size() == 0){
                param.workSupervisionFilter.setPageNumber(param.workSupervisionFilter.getPageNumber()-1);
                list = workSupervisionService.query(param.workSupervisionFilter);
            }
            Double pageTotal = Util.intGetTotalPage(workSupervisionService.queryForCount(param.workSupervisionFilter), param.workSupervisionFilter.getPageSize());
            HashMap<String, Object> map = new HashMap<>();
            map.put("list", list);
            map.put("pageTotal", pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @RequestMapping("/setPoint")
    public Result<String> setPoint(@RequestBody WorkParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            String str = workSupervisionService.setPoint(param.workSupervisionEntity);
            if (!"设置分数成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/importData")
    public Result<String> importData(@RequestParam(value = "file") MultipartFile file) {
        String str = workSupervisionService.importData(file);
        if (!"导入成功".equals(str)){
            return ResultUtils.getResults(Result.Status.ERROR,str);
        }
        return ResultUtils.getSuccessResults();
    }

    @PostMapping("/importPoint")
    public Result<String> importPoint(@RequestParam(value = "file") MultipartFile file) {
        String str = workSupervisionService.importPoint(file);
        if (!"导入成功".equals(str)){
            return ResultUtils.getResults(Result.Status.ERROR,str);
        }
        return ResultUtils.getSuccessResults();
    }

}

class WorkParam {
    public TokenEntity tokenEntity;
    public WorkSupervisionEntity workSupervisionEntity;
    public WorkSupervisionFilter workSupervisionFilter;
}
